<template>
  <a-table :rowSelection="{
        // selectedRowKeys: selectedRowKeys,
        onChange: onSelectChange,
        // getCheckboxProps:getCheckboxProps
      }"       :customRow="setRow"
           :columns="columns"
           :dataSource="goodss"
           rowKey="code"
  >
    <template #name2="{ record }"
    >
      {{record.total}}
    </template>

    <template #name="{ record }"
    >
      <a-input-number id="inputNumber" v-model:value="record.number"  :min="1" @change="onChange(record)"/>
    </template>

  </a-table>
  <div style="padding-left: 520px">
    <a-statistic title="总计" :precision="2" :value="this.money" />
  </div>

</template>

<script>
import 'ant-design-vue/dist/antd.css';
import store from "@/store";
import {reactive, ref} from "vue";
export default {
  name: "SellDisplay",

  data(){

    return{
      money:ref(0.0),
      goodss:ref([]),
      goods:reactive({
        key:0,
        code:'',
        goodsName:'',
        total:0.0,
        number:0

      }),


      columns: [
        {
          title: '编号',
          dataIndex: 'code',
          key: 'code',
        },
        {
          title: '商品名称',
          dataIndex: 'goodsName',
          key: 'goodsName',
        },
        {
          title: '总计/元',
          dataIndex: 'total',
          key: 'total',
          align: "center",
          slots: { customRender: "name2" }
        },
        {
          title: '数量',
          dataIndex: 'number',
          key: 'number',
          align: "center",
          slots: { customRender: "name" }
        }
      ]
    }
  }
  ,
mounted() {
if (store.getters.getCode===223){
  this.getGoods()
  store.commit('setCode','')

}else
  this.goodss=store.getters.getSell
  this.getMoney()

},

  methods:{

    getMoney(){

      let money=0.0
      for (let i in store.getters.getSell){
        money+=store.getters.getSell[i].total
        console.log(store.getters.getSell[i].total)
      }
      this.money=money

    },

    onChange(val){
      let price
      let data=store.getters.getSellPrice
      // for (let i in data){
      //   if (data[i].code===val.code){
      //     price=data[i].total/data[i].number
      //   }
      // }
      for (let i in data){
        if (data[i].code===val.code)
          price=data[i].price
      }
      val.total=price*val.number
      console.log(price)
      console.log(store.getters.getSell)
      this.getMoney()
    },


    getGoods() {
      let d1=store.getters.getData
      let d2=store.getters.getSell
      let is=true
      if(d1!==''){
        for (let i in d2){
          if (d1.code===d2[i].code){
            d2[i].number++
            d2[i].total+=d1.price
            is=false
          }
        }console.log('有吗')
        console.log(d2)
        if (is){
          console.log("添加")
          this.goods.key=store.getters.getData.code
          this.goods.code=store.getters.getData.code
          this.goods.goodsName=store.getters.getData.goodsName
          this.goods.total=store.getters.getData.price
          this.goods.number=1
          store.commit("setSell",this.goods)
        }else
          store.commit("setSellTN",d2)

      }
      let price={code:store.getters.getData.code,price:store.getters.getData.price}
      store.commit("setSellPrice",price)
      console.log(store.getters.getSellPrice)
      this.goodss=store.getters.getSell


    },

    onSelectChange(selectedRowKeys) {
      console.log("selectedRowKeys changed: ", selectedRowKeys);
      // this.selectedRowKeys = selectedRowKeys;
      // this.codeList=selectedRowKeys;
      // console.log(this.codeList)
      store.commit("setSellList",selectedRowKeys)
      console.log(store.getters.getSellList)
    },

    setRow (record) {
      return {
        on: {
          click: () => {
            let rowKeys = this.selectedRowKeys;
            if (rowKeys.length > 0 && rowKeys.includes(record.key)) {
              rowKeys.splice(rowKeys.indexOf(record.key), 1);
            } else {
              rowKeys.push(record.key);
            }
            this.selectedRowKeys = rowKeys;
            console.log(this.selectedRowKeys)
          }
        }
      };
    },
  }
}
</script>

<style scoped>

</style>